package edu.cicese.alz;

import edu.cicese.alz.swing.SwingUtil;
import edu.cicese.alz.swing.dialogs.CustomDialog;
import edu.cicese.alz.util.Utilities;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 28/05/12
 * Time: 06:00 PM
 */
public class FrameMain extends CustomDialog {
	private static FrameMain frmMain;

	private JToolBar tbMain;

	private FrameSubjects frmSubjects;
	private FrameCreateDSV frmCreateDSV;
	private FrameUploadDSV frmUploadDSV;
	private FrameSettings frmSettings;
	private FrameReport frmReport;
	private FrameNFCTags frmNFCTags;

	private static JTextArea txtLog;

	public static void main(String[] a) {
		SwingUtil.setUIManager();
		new FrameMain();
	}

	public FrameMain() {
		setTitle("InCense Data Processing");

		setSize(332, 94);
		setInitialLocation();

		initComponents();

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		setResizable(false);
		setAlwaysOnTop(true);
		setVisible(true);
	}


	private void initComponents() {
		JButton btnSubjects = SwingUtil.createButton("", "Manage subjects", getIcon("m_subjects"), Color.WHITE);
		JButton btnReports = SwingUtil.createButton("", "Create reports", getIcon("m_reports"), Color.WHITE);
		JButton btnNFCTags = SwingUtil.createButton("", "Manage NFC tags", getIcon("m_nfc_tags"), Color.WHITE);
		JButton btnCreateDSVs = SwingUtil.createButton("", "Create DSV files", getIcon("m_create_dsvs"), Color.WHITE);
		JButton btnUploadDSVs = SwingUtil.createButton("", "Upload DSV files", getIcon("m_upload_dsvs"), Color.WHITE);
		JButton btnPreferences = SwingUtil.createButton("", "Preferences", getIcon("m_preferences"), Color.WHITE);

		btnSubjects.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showFrameSubjects();
			}
		});
		btnReports.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showFrameReport();
			}
		});
		btnNFCTags.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showFrameNFCTags();
			}
		});
		btnCreateDSVs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showFrameCreateDSVs();
			}
		});
		btnUploadDSVs.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showFrameUploadDSVs();
			}
		});
		btnPreferences.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showFramePreferences();
			}
		});

		Box boxButtons = Box.createHorizontalBox();
		boxButtons.add(btnSubjects);
		boxButtons.add(Box.createHorizontalStrut(3));
		boxButtons.add(btnReports);
		boxButtons.add(Box.createHorizontalStrut(3));
		boxButtons.add(btnCreateDSVs);
		boxButtons.add(Box.createHorizontalStrut(3));
		boxButtons.add(btnUploadDSVs);
		boxButtons.add(Box.createHorizontalStrut(3));
		boxButtons.add(btnNFCTags);
		boxButtons.add(Box.createHorizontalStrut(3));
		boxButtons.add(btnPreferences);

//		Box boxButtons1 = Box.createVerticalBox();
//		boxButtons1.add(btnSubjects);
//		boxButtons1.add(btnNFCTags);
//		boxButtons1.add(btnUploadDSVs);
//
//		Box boxButtons2 = Box.createVerticalBox();
//		boxButtons2.add(btnReports);
//		boxButtons2.add(btnCreateDSVs);
//		boxButtons2.add(btnPreferences);
//
//		Box boxButtons = Box.createHorizontalBox();
//		boxButtons.add(boxButtons1);
//		boxButtons.add(boxButtons2);

		JPanel pContent = new JPanel();
		pContent.add(boxButtons);
		pContent.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
		setContentPane(pContent);


//		tbMain = new JToolBar();
//		tbMain.setFloatable(false);
//		JButton btnSubject = new JButton();
//		addButton(btnSubject, "", "Manage subjects", "b_new", tbMain);
//		btnSubject.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				showFrameSubjects();
//			}
//		});
//
//		JButton btnCreateDSV = new JButton();
//		addButton(btnCreateDSV, "", "Create DSV files", "b_delete", tbMain);
//		btnCreateDSV.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				showFrameCreateDSVs();
//			}
//		});
//
//		JButton btnUploadDSV = new JButton();
//		addButton(btnUploadDSV, "", "Upload DSV files to database", "b_save", tbMain);
//		btnUploadDSV.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				showFrameUploadDSVs();
//			}
//		});
//
//		JButton btnSettings = new JButton();
//		addButton(btnSettings, "", "Settings", "b_cancel", tbMain);
//		btnSettings.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				showFrameSettings();
//			}
//		});
//
//		JPanel pToolBar = new JPanel(new BorderLayout());
//		pToolBar.add(tbMain, BorderLayout.NORTH);
//
//		txtLog = new JTextArea();
//		JScrollPane spLog = new JScrollPane(txtLog);
//		spLog.setPreferredSize(new Dimension(250, 250));
//
//		JPanel pLog = new JPanel(new BorderLayout());
//		pLog.add(spLog, BorderLayout.NORTH);
//
//
//		JPanel pContent = new JPanel(new BorderLayout());
//		pContent.add(pToolBar, BorderLayout.NORTH);
//		pContent.add(pLog, BorderLayout.CENTER);
//		setContentPane(pContent);


//		JMenuBar menuBar = new JMenuBar();
//        ImageIcon iconNew = new ImageIcon(getClass().getClassLoader().getResource("icons/new.png"));
//        ImageIcon iconNewGroup = new ImageIcon(getClass().getClassLoader().getResource("icons/newGroup.png"));
//        ImageIcon iconEdit = new ImageIcon(getClass().getClassLoader().getResource("icons/edit.png"));
//        ImageIcon iconDelete = new ImageIcon(getClass().getClassLoader().getResource("icons/delete.png"));
//        ImageIcon iconExit = new ImageIcon(getClass().getResource("exit.png"));

//        JMenu subject = new JMenu("");
//        subject.setMnemonic(KeyEvent.VK_P);
//		subject.addActionListener(new ActionListener() {
//            public void actionPerformed(ActionEvent event) {
//	            System.out.println("click");
//            }
//        });
//
//		tbMain.add(subject);

//        JMenuItem mSubjects = new JMenuItem("Subjects");
//        mSubjects.setMnemonic(KeyEvent.VK_N);
//		mSubjects.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent event) {
//				System.out.println("subjects");
//			}
//		});
//
//		JMenu mReports = new JMenu("Reports");
//        mReports.setMnemonic(KeyEvent.VK_P);
//		mReports.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent event) {
//				System.out.println("reports");
//			}
//		});
		
//        JMenuItem m = new JMenuItem("New Group", iconNewGroup);
//        m.setMnemonic(KeyEvent.VK_G);
//
//        JMenuItem subjectEdit = new JMenuItem("Edit", iconEdit);
//        subjectEdit.setMnemonic(KeyEvent.VK_E);
//
//		menuBar.add(mSubjects);
//
//		pToolBar.add(menuBar);
//
//        JMenuItem subjectNewGroup = new JMenuItem("New Group", iconNewGroup);
//        subjectNewGroup.setMnemonic(KeyEvent.VK_G);
//
//        JMenuItem subjectEdit = new JMenuItem("Edit", iconEdit);
//        subjectEdit.setMnemonic(KeyEvent.VK_E);
//
//		JMenuItem subjectDelete = new JMenuItem("Delete", iconDelete);
//        subjectDelete.setMnemonic(KeyEvent.VK_D);

        /*JMenuItem fileExit = new JMenuItem("Exit", iconExit);
        fileExit.setMnemonic(KeyEvent.VK_C);
        fileExit.setToolTipText("Exit application");
        fileExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
		        ActionEvent.CTRL_MASK));

        fileExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }

        });*/

//        subject.add(subjectNew);
//        subject.add(subjectNewGroup);
//		subject.addSeparator();
//		subject.add(subjectEdit);
//        subject.add(subjectDelete);
//
//        menuBar.add(subjectNew);
//
//        setJMenuBar(menuBar);



	}

	public void setInitialLocation() {
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle window = getBounds();
		setLocation((screen.width - window.width) / 2, 50);
	}


	private void showFrameSubjects() {
		if (frmSubjects == null) {
			frmSubjects = new FrameSubjects();
		}
		frmSubjects.setVisible(true);
	}

	private void showFrameReport() {
		if (frmReport == null) {
			frmReport = new FrameReport();
		}
		frmReport.setVisible(true);
	}

	private void showFrameNFCTags() {
		if (frmNFCTags == null) {
			frmNFCTags = new FrameNFCTags();
		}
		frmNFCTags.setVisible(true);
	}

	private void showFrameCreateDSVs() {
		if (frmCreateDSV == null) {
			frmCreateDSV = new FrameCreateDSV();
		}
		frmCreateDSV.setVisible(true);
	}

	private void showFrameUploadDSVs() {
		if (frmUploadDSV == null) {
			frmUploadDSV = new FrameUploadDSV();
		}
		frmUploadDSV.setVisible(true);
	}

	private void showFramePreferences() {
		if (frmSettings == null) {
			frmSettings = new FrameSettings();
		}
		frmSettings.setVisible(true);
	}


	public static void printLog(String msg) {
		Calendar calendar = Calendar.getInstance();
		txtLog.append("[" +
				Utilities.addZeros(calendar.get(Calendar.HOUR_OF_DAY), 2) + ":" +
				Utilities.addZeros(calendar.get(Calendar.MINUTE), 2) + ":" +
				Utilities.addZeros(calendar.get(Calendar.SECOND), 2) + "] " + msg);
	}



	/*private AbstractButton createReportButton() {
        final ColoredToggleButton btnCapture = new ColoredToggleButton(false);
		addToggleButton(btnCapture, "Report", "tooltip", "b_new", tbMain);
		btnCapture.addListener();
		JPopupMenu popupMenu = new JPopupMenu();
		popupMenu.setBackground(SwingUtil.cBackground);
		popupMenu.add(new JMenuItem(new AbstractAction("Option 1") {
	        public void actionPerformed(ActionEvent e) {
		        btnCapture.setSelected(false);
	        }
        }));
        popupMenu.add(new JMenuItem(new AbstractAction("Option 2") {
	        public void actionPerformed(ActionEvent e) {
		        btnCapture.setSelected(false);
	        }
        }));
//
//		        final JButton button = new JButton("Options");
//		        button.addMouseListener(new MouseAdapter() {
//		            public void mousePressed(MouseEvent e) {
//		                ColoredToggleButton.this.popupMenu.show(e.getComponent(), e.getX(), e.getY());
//		            }
//		        });

		btnCapture.setPopUpMenu(popupMenu);

//        btnCapture.addItemListener(new ItemListener() {
//            public void itemStateChanged(ItemEvent e) {
//	            createAndShowMenu((JComponent) e.getSource(), btnCapture);
//	            if (e.getStateChange() == ItemEvent.SELECTED) {
//                }
//            }
//        });
//        btnCapture.setFocusable(false);
//        btnCapture.setHorizontalTextPosition(SwingConstants.LEADING);
        return btnCapture;
    }*/

    private void createAndShowMenu(final JComponent component, final AbstractButton moreButton) {
        JPopupMenu menu = new JPopupMenu();
	    menu.setSize(new Dimension(100, 50));
        menu.add(new JMenuItem("Black"));
        menu.add(new JMenuItem("Red"));

        menu.addPopupMenuListener(new PopupMenuListener() {
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                moreButton.setSelected(false);
            }

            public void popupMenuCanceled(PopupMenuEvent e) {
                moreButton.setSelected(false);
            }
        });

        menu.show(component, 0, component.getHeight());
    }
}
